/*
 * Copyright (c) 2019-2020, https://api.iot.10086.cn/(https://gitee.com/cmcc-iot-api/iot-card-open-application/).
 */
package cn.iot.card.open.sim.controller;

import cn.iot.card.open.context.LoginContextHolder;
import cn.iot.card.open.context.model.LoginUser;
import cn.iot.card.open.exception.enums.BizExceptionEnum;
import cn.iot.card.open.modular.entity.AccountCard;
import cn.iot.card.open.modular.entity.Card;
import cn.iot.card.open.modular.entity.UserGroupRl;
import cn.iot.card.open.modular.log.ApiBussinessLog;
import cn.iot.card.open.modular.service.AccountCardService;
import cn.iot.card.open.modular.service.CardService;
import cn.iot.card.open.modular.service.UserService;
import cn.iot.card.open.pojo.page.LayuiPageFactory;
import cn.iot.card.open.pojo.page.LayuiPageInfo;
import cn.iot.card.open.sim.dto.siminfo.GprsLimitSpeedDTO;
import cn.iot.card.open.sim.dto.siminfo.GprsPackageOrderDTO;
import cn.iot.card.open.sim.dto.siminfo.GprsPccLimitSpeedDTO;
import cn.iot.card.open.sim.dto.siminfo.SimInfoDTO;
import cn.iot.card.open.sim.service.SimInfoForGprsService;
import cn.iot.card.open.util.CommonUtil;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import cn.stylefeng.roses.kernel.model.exception.ServiceException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 物联卡综合信息---GPRS
 *
 * @author zhanlele
 * @date 20200312 10:45:13
 * @modify 20200312 zhanlele v1.1.2 创建
 * @since v1.1.2
 */
@Controller
@RequestMapping("/simInfoForGprs")
public class SimInfoForGprsController {

    private static String PREFIX = "/modular/sim/general/gprs/";

    @Autowired
    private SimInfoForGprsService simInfoForGprsService;

    @Autowired
    private CardService cardService;

    @Autowired
    private UserService userService;

    @Autowired
    private AccountCardService accountCardService;

    /**
     * 跳转GPRS上网功能重置页面
     *
     * @return 返回 string
     * @author zhanlele
     * @date 20200312 10:51:01
     * @modify 20200312 zhanlele v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping("/toResetGprsNetFunction")
    public String toResetGprsNetFunction() {
        return PREFIX + "resetGprsNetFunction.html";
    }

    /**
     * GPRS上网功能重置
     *
     * @param simInfoDTO 卡号和卡类型
     * @return 返回 response data
     * @throws Exception
     * @author zhanlele
     * @date 20200312 14:44:38
     * @modify 20200312 zhanlele v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping("/resetGprsNetFunction")
    @ApiBussinessLog(value = "物联卡GPRS上网功能重置")
    @ResponseBody
    public ResponseData resetGprsNetFunction(SimInfoDTO simInfoDTO) throws Exception {
        String cardType = simInfoDTO.getCardType();
        String cardNum = simInfoDTO.getCardNum();
        Card card = cardService.findCard(cardType, cardNum);
        if (null == card) {
            throw new ServiceException(BizExceptionEnum.CARD_NOT_EXIT_IN_SYSTEM);
        }

        LoginUser loginUser = LoginContextHolder.getContext().getUser();
        UserGroupRl userGroupRl = userService.getUserGroupRlByUserAndCard(loginUser.getId(), card.getCardId());
        if (null == userGroupRl) {
            throw new ServiceException(BizExceptionEnum.CARD_NOT_BELONG_USER);
        }

        AccountCard accountCard = accountCardService.getBaseMapper().findByCardId(card.getCardId());
        String accountId = String.valueOf(accountCard.getAccountId());
        String transId = CommonUtil.generateTransId(accountId);

        return simInfoForGprsService.resetGprsNetFunction(accountId, transId, cardType, cardNum);
    }

    /**
     * 跳转GPRS(自主)限速页面
     *
     * @return 返回 string
     * @author zhanlele
     * @date 20200312 14:57:34
     * @modify 20200312 zhanlele v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping("/toGprsLimitSpeed")
    public String toGprsLimitSpeed() {
        return PREFIX + "gprsLimitSpeed.html";
    }

    /**
     * GPRS(自主)限速
     *
     * @param gprsLimitSpeedDTO
     * @return 返回 response data
     * @throws Exception
     * @author zhanlele
     * @date 20200312 15:56:36
     * @modify 20200312 zhanlele v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping("/gprsLimitSpeed")
    @ApiBussinessLog(value = "物联卡 GPRS（自主）限速")
    @ResponseBody
    public ResponseData gprsLimitSpeed(GprsLimitSpeedDTO gprsLimitSpeedDTO) throws Exception {
        Card card = cardService.findCard(gprsLimitSpeedDTO.getCardType(), gprsLimitSpeedDTO.getCardNum());
        if (null == card) {
            throw new ServiceException(BizExceptionEnum.CARD_NOT_EXIT_IN_SYSTEM);
        }

        LoginUser loginUser = LoginContextHolder.getContext().getUser();
        UserGroupRl userGroupRl = userService.getUserGroupRlByUserAndCard(loginUser.getId(), card.getCardId());
        if (null == userGroupRl) {
            throw new ServiceException(BizExceptionEnum.CARD_NOT_BELONG_USER);
        }

        AccountCard accountCard = accountCardService.getBaseMapper().findByCardId(card.getCardId());
        String accountId = String.valueOf(accountCard.getAccountId());
        String transId = CommonUtil.generateTransId(accountId);

        return simInfoForGprsService.gprsLimitSpeed(accountId, transId, gprsLimitSpeedDTO);
    }

    /**
     * 跳转GPRS（PCC策略）限速页面
     *
     * @return 返回 string
     * @author zhanlele
     * @date 20200312 16:28:52
     * @modify 20200312 zhanlele v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping("/toGprsPccLimitSpeed")
    public String toGprsPccLimitSpeed() {
        return PREFIX + "gprsPccLimitSpeed.html";
    }

    /**
     * GPRS（PCC策略）限速
     *
     * @param gprsPccLimitSpeedDTO
     * @return 返回 response data
     * @throws Exception
     * @author zhanlele
     * @date 20200312 17:50:24
     * @modify 20200312 zhanlele v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping("/gprsPccLimitSpeed")
    @ApiBussinessLog(value = "物联卡GPRS（PCC策略）限速")
    @ResponseBody
    public ResponseData gprsPccLimitSpeed(GprsPccLimitSpeedDTO gprsPccLimitSpeedDTO) throws Exception {
        Card card = cardService.findCard(gprsPccLimitSpeedDTO.getCardType(), gprsPccLimitSpeedDTO.getCardNum());
        if (null == card) {
            throw new ServiceException(BizExceptionEnum.CARD_NOT_EXIT_IN_SYSTEM);
        }

        LoginUser loginUser = LoginContextHolder.getContext().getUser();
        UserGroupRl userGroupRl = userService.getUserGroupRlByUserAndCard(loginUser.getId(), card.getCardId());
        if (null == userGroupRl) {
            throw new ServiceException(BizExceptionEnum.CARD_NOT_BELONG_USER);
        }

        AccountCard accountCard = accountCardService.getBaseMapper().findByCardId(card.getCardId());
        String accountId = String.valueOf(accountCard.getAccountId());
        String transId = CommonUtil.generateTransId(accountId);

        return simInfoForGprsService.gprsPccLimitSpeed(accountId, transId, gprsPccLimitSpeedDTO);
    }

    /**
     * 跳转流量叠加包订购页面
     *
     * @return 返回 string
     * @author zhanlele
     * @date 20200312 17:59:13
     * @modify 20200312 zhanlele v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping("/toGprsPackageOrder")
    public String toGprsPackageOrder() {
        return PREFIX + "gprsPackageOrder.html";
    }

    /**
     * 流量叠加包订购
     *
     * @param gprsPackageOrderDTO
     * @return 返回 response data
     * @author zhanlele
     * @date 20200312 18:05:43
     * @modify 20200312 zhanlele v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping("/gprsPackageOrder")
    @ApiBussinessLog(value = "物联卡流量叠加包订购")
    @ResponseBody
    public ResponseData gprsPackageOrder(GprsPackageOrderDTO gprsPackageOrderDTO) throws Exception {
        Card card = cardService.verifyCardInfo(gprsPackageOrderDTO.getMsisdn(), null, null);
        if (null == card) {
            throw new ServiceException(BizExceptionEnum.CARD_NOT_EXIT_IN_SYSTEM);
        }

        LoginUser loginUser = LoginContextHolder.getContext().getUser();
        UserGroupRl userGroupRl = userService.getUserGroupRlByUserAndCard(loginUser.getId(), card.getCardId());
        if (null == userGroupRl) {
            throw new ServiceException(BizExceptionEnum.CARD_NOT_BELONG_USER);
        }

        AccountCard accountCard = accountCardService.getBaseMapper().findByCardId(card.getCardId());
        String accountId = String.valueOf(accountCard.getAccountId());
        String transId = CommonUtil.generateTransId(accountId);

        return simInfoForGprsService.gprsPackageOrder(accountId, transId, gprsPackageOrderDTO);
    }

    /**
     * APN信息
     *
     * @param msisdn 物联卡号码
     * @param iccid 集成电路卡识别码
     * @param imsi 国际移动用户识别码
     * @return 返回 layui page info
     * @throws Exception
     * @author zhanlele
     * @date 20200313 17:20:49
     * @modify 20200313 zhanlele v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping("/singleCardApnInfo")
    @ResponseBody
    public LayuiPageInfo singleCardApnInfo(@RequestParam(required = false) String msisdn,
                                           @RequestParam(required = false) String iccid,
                                           @RequestParam(required = false) String imsi) throws Exception {
        if (StringUtils.isBlank(msisdn) && StringUtils.isBlank(iccid) && StringUtils.isBlank(imsi)) {
            return LayuiPageFactory.createEmptyPageInfo();
        }

        Card card = cardService.verifyCardInfo(msisdn, iccid, imsi);
        if (null == card) {
            return LayuiPageFactory.createBizExceptionMsg(BizExceptionEnum.CARD_NOT_EXIT_IN_SYSTEM);
        }

        LoginUser loginUser = LoginContextHolder.getContext().getUser();
        UserGroupRl userGroupRl = userService.getUserGroupRlByUserAndCard(loginUser.getId(), card.getCardId());
        if (null == userGroupRl) {
            return LayuiPageFactory.createBizExceptionMsg(BizExceptionEnum.CARD_NOT_BELONG_USER);
        }

        AccountCard accountCard = accountCardService.getBaseMapper().findByCardId(card.getCardId());
        String accountId = String.valueOf(accountCard.getAccountId());
        String transId = CommonUtil.generateTransId(accountId);

        return simInfoForGprsService.singleCardApnInfo(msisdn, iccid, imsi, transId, accountId);
    }

    /**
     * 套餐内用量
     *
     * @param msisdn 物联卡号码
     * @param iccid 集成电路卡识别码
     * @param imsi 国际移动用户识别码
     * @return 返回 layui page info
     * @throws Exception
     * @author zhanlele
     * @date 20200314 10:49:05
     * @modify 20200314 zhanlele v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping("/packageGprsUsed")
    @ResponseBody
    public LayuiPageInfo packageGprsUsed(@RequestParam(required = false) String msisdn,
                                         @RequestParam(required = false) String iccid,
                                         @RequestParam(required = false) String imsi) throws Exception {
        if (StringUtils.isBlank(msisdn) && StringUtils.isBlank(iccid) && StringUtils.isBlank(imsi)) {
            return LayuiPageFactory.createEmptyPageInfo();
        }

        Card card = cardService.verifyCardInfo(msisdn, iccid, imsi);
        if (null == card) {
            return LayuiPageFactory.createBizExceptionMsg(BizExceptionEnum.CARD_NOT_EXIT_IN_SYSTEM);
        }

        LoginUser loginUser = LoginContextHolder.getContext().getUser();
        UserGroupRl userGroupRl = userService.getUserGroupRlByUserAndCard(loginUser.getId(), card.getCardId());
        if (null == userGroupRl) {
            return LayuiPageFactory.createBizExceptionMsg(BizExceptionEnum.CARD_NOT_BELONG_USER);
        }

        AccountCard accountCard = accountCardService.getBaseMapper().findByCardId(card.getCardId());
        String accountId = String.valueOf(accountCard.getAccountId());
        String transId = CommonUtil.generateTransId(accountId);

        return simInfoForGprsService.packageGprsUsed(accountId, transId, msisdn, iccid, imsi);
    }

    /**
     * 流量池内用量
     *
     * @param msisdn 物联卡号码
     * @param iccid 集成电路卡识别码
     * @param imsi 国际移动用户识别码
     * @return 返回 layui page info
     * @throws Exception
     * @author zhanlele
     * @date 20200314 10:59:03
     * @modify 20200314 zhanlele v1.1.2 创建
     * @since v1.1.2
     */
    @RequestMapping("/singleCardPoolUsed")
    @ResponseBody
    public LayuiPageInfo singleCardPoolUsed(@RequestParam(required = false) String msisdn,
                                            @RequestParam(required = false) String iccid,
                                            @RequestParam(required = false) String imsi) throws Exception {
        if (StringUtils.isBlank(msisdn) && StringUtils.isBlank(iccid) && StringUtils.isBlank(imsi)) {
            return LayuiPageFactory.createEmptyPageInfo();
        }

        Card card = cardService.verifyCardInfo(msisdn, iccid, imsi);
        if (null == card) {
            return LayuiPageFactory.createBizExceptionMsg(BizExceptionEnum.CARD_NOT_EXIT_IN_SYSTEM);
        }

        LoginUser loginUser = LoginContextHolder.getContext().getUser();
        UserGroupRl userGroupRl = userService.getUserGroupRlByUserAndCard(loginUser.getId(), card.getCardId());
        if (null == userGroupRl) {
            return LayuiPageFactory.createBizExceptionMsg(BizExceptionEnum.CARD_NOT_BELONG_USER);
        }

        AccountCard accountCard = accountCardService.getBaseMapper().findByCardId(card.getCardId());
        String accountId = String.valueOf(accountCard.getAccountId());
        String transId = CommonUtil.generateTransId(accountId);

        return simInfoForGprsService.singleCardPoolUsed(accountId, transId, msisdn, iccid, imsi);
    }
}
